package com.clf.data.redis.core.json;

import org.springframework.data.redis.serializer.RedisSerializer;
import org.springframework.util.Assert;

import java.nio.charset.Charset;

/**
 * key解析器
* @author <a href="mailto:zhudelie@zuozh.com">zhudelie</a>
* @version 
* @since JDK 1.6
* create on 2017年3月31日
* Copyright 2017 ZZJR All Rights Reserved.
*
*/
public class KeySerializer implements RedisSerializer<Object> {

	private final Charset charset;

	public KeySerializer() {
		this(Charset.forName("UTF8"));
	}

	public KeySerializer(Charset charset) {
		Assert.notNull(charset, "Charset must not be null!");
		this.charset = charset;
	}

	@Override
	public Object deserialize(byte[] bytes) {
		return (bytes == null ? null : FastJsonSerializerStringPool.bytes2String(bytes));
	}

	@Override
	public byte[] serialize(Object obj) {
		if(obj == null){
			return null;
		}
		String str;
		if(obj instanceof String){
			str = (String) obj;
		}else{
			str = obj.toString();
		}
		return str.getBytes(charset);
	}

}
